const vscode = require('vscode');

/**
 * 获取工作区根路径，末尾不含'/'
 * @returns {string}
 */
const getRootPath = () => vscode.workspace?.workspaceFolders?.[0]?.uri?.fsPath;

const settingsJson = { password: '' };
const setSettingsPassword = i => settingsJson.password = i;
/**
 * 实时返回配置文件settings.json信息
 * @returns {{
*     username: string,
*     password: string,
*     url: string,
*     projectkey: string,
* }}
*/
const getSettingConfig = () => {
    const configFunc = vscode.workspace.getConfiguration();

    return {
        ...settingsJson,
        username: configFunc.get('operatorlint.username'),
        url: configFunc.get('operatorlint.url'),
        projectkey: configFunc.get('operatorlint.projectkey'),
    };
};

/**
 * 根据issues里面的component推测出完整的文件路径
 * @param {*} e 
 * @returns {string}
 */
const getIssuesFsPath = e => {
    const rootPath = getRootPath();
    const settingsConfig = getSettingConfig();

    return rootPath + '/' + e.replace(settingsConfig.projectkey + ':', '');
};

/**
 * 返回没有projectkey的文件路径
 * @param {string} path 
 */
const fixFilePathPre = path => {
    const { projectkey } = getSettingConfig();

    if (path.length <= projectkey.length) return path;

    return path.replace(projectkey + ':', '');
};

module.exports = {
    getRootPath,
    getSettingConfig,
    setSettingsPassword,
    getIssuesFsPath,
    fixFilePathPre
}